package com.sentiment.database.bean;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.bson.Document;
import org.bson.types.ObjectId;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.sentiment.bean.LogOperation;
import com.sentiment.database.dbUtil.DbUtil;
import com.sentiment.tools.Format;

public class UserLog{

	@Override
	public String toString() {
		return "UserLog [_id=" + _id + ", user_id=" + user_id
				+ ", operationdate=" + operationdate + ", operation="
				+ operation + ", ipadd=" + ipadd + "]";
	}
	
	public static final String dbName = "UserDB";
	public static final String collection = "UserLog";
	
	private ObjectId _id;
	private String user_id;
	private String operationdate;
	private String operation;
	private String ipadd;
	
	public UserLog(Document doc){
		super();
		_id = doc.getObjectId("_id");
		user_id = doc.getString("user_id");
		operationdate = doc.getString("operationdate");
		operation = doc.getString("operation");
		ipadd = doc.getString("ipadd");
		
	}
	
	public Document toDocument(){
		return new Document("user_id", user_id).append("operationdate", operationdate)
		.append("operation", operation).append("ipadd", ipadd);
	}

	public UserLog(String user_id, String operationdate, String operation, String ipadd) {
		super();
		this.user_id = user_id;
		this.operationdate = operationdate;
		this.operation = operation;
		this.ipadd = ipadd;
	}

	public UserLog() {
		// TODO Auto-generated constructor stub
	}
	@JsonIgnore
	public String get_id() {
		return _id.toString();
	}

	public void set_id(String _id) {
		this._id = new ObjectId(_id);
	}
	@JsonIgnore
	public String getUser_id() {
		return user_id;
	}

	public void setUser_id(String user_id) {
		this.user_id = user_id;
	}

	public String getUsername(){
		User user = new User();
		user = user.getUserByUser_id(this.getUser_id());
		return user.getUsercode();
	}
	
	
	public String getOperationdate() {
		return operationdate;
	}

	public void setOperationdate(Date operationdate) {
		this.operationdate = Format.date2String(operationdate, 3);
	}

	public String getOperation() {
		return operation;
	}

	public void setOperation(String operation) {
		this.operation = operation;
	}

	public String getIpadd() {
		return ipadd;
	}

	public void setIpadd(String ipadd) {
		this.ipadd = ipadd;
	}

	/**
	 * 用于测试时将数据写入数据库
	 */
	public void test(){
		
		DbUtil db = new DbUtil();
		db.openConnection(dbName, collection);
		
		List<String> users = new ArrayList<>();
		users.add("user");
		users.add("user_pay");
		users.add("admin");
		for(int j = 0; j < users.size(); j++){
			
			User user = new User();
			user = user.getUserByUserCode(users.get(j));
			String user_id = user.get_id();
			
			UserLog log = new UserLog(user_id, Format.date2String(new Date(), 3), LogOperation.login, "127.168.1.1");
			db.insert(log.toDocument());
			
			log = new UserLog(user_id, Format.date2String(new Date(), 3), LogOperation.loginout, "127.168.1.1");
			db.insert(log.toDocument());
			
			log = new UserLog(user_id, Format.date2String(new Date(), 3), LogOperation.findpassword, "127.168.1.1");
			db.insert(log.toDocument());
			
		}
		
		db.closeConnection();
	}
	
	
	@Override
	public boolean equals(Object obj) {

		 if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        UserLog other = (UserLog) obj;
        if (_id == null) {
            if (other._id != null)
                return false;
        } else if (!_id.equals(other._id))
            return false;
        
        if (operationdate == null) {
            if (other.operationdate != null)
                return false;
        } else if (!operationdate.equals(other.operationdate))
            return false;
        
        if (operation == null) {
            if (other.operation != null)
                return false;
        } else if (!operation.equals(other.operation))
            return false;
        
        if (ipadd == null) {
            if (other.ipadd != null)
                return false;
        } else if (!ipadd.equals(other.ipadd))
            return false;
        
        return true;
	}
	
}
